﻿using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for ProductionOrder.xaml
    /// </summary>
    public partial class TKDBOX
    {
        public TKDBOX()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            InitializeComponent();
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            if (dwMaster != null)
            {
                KD15KEY = dwMaster["KDKUNDNR"].ToString() + "VK".PadRight(8);//先通知新建
                KVKEY = TMV.KDKUNDNR + "   " + "001";
                已付款 = Convert.ToDouble(dwMaster["KDVERSUMLTO"].ToString());
                待处理现金 = Convert.ToDouble(dwMaster["KDAUFBEST"].ToString());
                已开票 = Convert.ToDouble(dwMaster["KDVERSUML"].ToString());
                KEY = dwMaster["KDKUNDNR"].ToString();
            }
            else
            {
                KVKEY = "";
                KD15KEY = "";
                已付款 = 0;
                待处理现金 = 0;
                已开票 = 0;
                KEY = "";
            }
        }
         
        public override void On已开票Chg(double value)
        {
            if (MV == null)
                return;
            if (dwMaster == null)
                return;
            TMV.KDVERSUML = value;
            SWING = 已开票 - (待处理现金 + 已付款);
            CREDIT = ((VKDSTAMM)MV).KDKREDLIM1 - SWING;
        }

        public override void On待处理现金Chg(double value)
        {
            if (MV == null)
                return;
            if (dwMaster == null)
                return;
            TMV.KDAUFBEST = value; 
            TMV.UpdateKDSTAMM(dwMaster, 0); 
            //SWING = 已开票 - (待处理现金 + 已付款);
            SWING = 已开票 - (待处理现金);
            CREDIT = ((VKDSTAMM)MV).KDKREDLIM1 - SWING;
        }

        public override void On已付款Chg(double value)
        {
            if (MV == null)
                return;
            if (dwMaster == null)
                return;
            TMV.KDVERSUMLTO = value;
            SWING = 已开票 - (待处理现金);
            CREDIT = ((VKDSTAMM)MV).KDKREDLIM1 - SWING;
        }

        public override void OnCREDITChg(double value)
        {
            if (MV == null)
                return;
            if (dwMaster == null)
                return;
            if (value < 0)
            {
                TMV.KDSPLIEFKZ = "J";
                KDBLOCK = true;
            }
            else
            {
                TMV.KDSPLIEFKZ = " ";
                KDBLOCK = false;
            }

        }
        public override void LOADBYAUF()
        {
            if (!IsCanLoad)
                return; 
            base.LOADBYAUF();
        }

        private void VR02Command(object sender, RoutedEventArgs e)
        {
            VICC_WFEVENTS.SVWF.VR02(RUNID, "K".PadRight(30));
        }

        private void VK02Command(object sender, RoutedEventArgs e)
        {
            VICC_WFEVENTS.SVWF.VK02(RUNID);
        }

        public override void OnSave()
        {
            if (!IsSaved)
                return;
            if (dwMaster == null)
                return;
            TMV.UpdateKDSTAMM(dwMaster, 0); 
            PM["KUNDNR"] = dwMaster["KDKUNDNR"].ToString();
            TMV.KDUPDATE(PM);
            string _key = KEY;
            KEY = "";
            KEY = _key;//触发关联界面更改
            base.OnSave();
        }
        public override void OnReload(bool value)
        {
            if (!value)
                return;
            LOADBYAUF();
            base.OnReload(value);
        } 
        public override void OnKDBLOCKChg(bool value)
        {
            if (!value)
                return;
            dwData = TMV.VK.TBMain.NewRow();
            dwData["VKKMANDANT"] = app.Mandant; ;
            dwData["VKKKUNDNR"] = KEY;
            SResult = TMV.VK.QueryVKKOPF(dwData, 3);
            TBTemp = TMV.VK.TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            string vkblock = "";
            foreach (DataRow row in TBTemp.Rows)
            {
                vkblock = "K" + row["VKKAUFKRS"].ToString() + row["VKKAUFLFD"].ToString();
                if (row["VKKKDSTA"].ToString() == "11") break;
                row["VKKKDSTA"] = 11;
                TMV.VK.UpdateVKKOPF(row, app.company, 6);
                break;
            }
            VKBLOCK = vkblock;
            base.OnKDBLOCKChg(value);
        }
    }
}
